package com.liuke.study.thread;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;

import javax.print.attribute.standard.Finishings;
import java.security.PrivateKey;
import java.util.Random;

/**
 * TODO
 *
 * @author wangjwy
 * @date 2019/9/20 10:21
 * @since 1.0.0
 */
public class TestWait {

	private volatile int i = 0;

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	public static void main(String[] args) throws InterruptedException {
		// TestB testB = new TestB();
		// new Thread(new MyThread4(new TestB())).start();
		// new Thread(new MyThread5(new TestB())).start();
		// caculate(134788, 25, 8);
		// caculate(13, 12, 2);
		// for (int i = 12; i < Integer.MAX_VALUE; i++) {
		// 	caculate(i, 12, new Random().nextInt((i-1)/12 + 1) + 1);
		//
		// }
		Te te = new Te(0);
		for (int i = 0; i < 100; i++) {
			test2();
		}



		// for (int j = 0; j < 100; j++) {
		// 	new TestWait().test();
		// }

	}

	private static void test2() throws InterruptedException {
		final int[] i = { 0 };
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int j = 0; j < 100; j++) {
					i[0]++;
				}
			}
		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int j = 0; j < 100; j++) {
					i[0]++;
				}
			}
		}).start();

		Thread.sleep(1000);
		System.out.println(i[0]);
	}

	private  void test() throws InterruptedException {
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int j = 0; j < 100; j++) {
					i++;
				}
			}
		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int j = 0; j < 100; j++) {
					i++;
				}
			}
		}).start();

		Thread.sleep(1000);
		System.out.println(i);
	}

	private static Integer caculate(int totalSize, int pageSize, int pageNo) {

		try {

			if (totalSize == 0 || totalSize < pageSize){
				System.out.println("page : " + 1);
				System.out.println("pageSize : " + pageSize);
				return null;
			}

			int begin = totalSize - (pageNo) * pageSize + 1;

			int start =  begin <= 1?1: begin;
			int end = begin + pageSize - 1;
			if (totalSize%pageSize == 0) {
				// System.out.println("page : " + (start-1)/pageSize + 1);
				// System.out.println("pageSize : " + pageSize);
				return null;
			}

			for (int i = pageSize; i <= 50; i++) {
				if ((start-1)/i + 1 == (end - 1)/i + 1){
					// System.out.println("page : " + ((start-1)/i + 1));
					// System.out.println("pageSize : " + i);
					return null;
				}
			}

			throw new RuntimeException();
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
	}

}



class Te {

	public Integer getI() {
		return i;
	}

	public void setI(Integer i) {
		this.i = i;
	}

	private Integer i;
	public Te (Integer i) {
		this.i = i;
	}

}

class TestB{


	public void test1() throws Exception {
		synchronized (this) {
			System.out.println(Thread.currentThread().getName() + "start -- wait");
			this.wait();
			System.out.println(Thread.currentThread().getName() + "end -- wait");
			Thread.sleep(3000);
			this.notify();
		}
	}

	public void test2() throws Exception {
		synchronized (this) {
			System.out.println(Thread.currentThread().getName() + "start -- sleep");
			Thread.sleep(5000);
			System.out.println(Thread.currentThread().getName() + "start -- notify");
			this.notify();
			System.out.println(Thread.currentThread().getName() + "end -- notify");
			this.wait();
		}
	}

}


class MyThread4 implements Runnable {

	private TestB testB;

	public MyThread4(TestB testB) {
		this.testB = testB;
	}

	@Override
	public void run() {
		try {
			testB.test1();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


class MyThread5 implements Runnable {

	private TestB testB;

	public MyThread5(TestB testB) {
		this.testB = testB;
	}

	@Override
	public void run() {
		try {
			testB.test2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}